<?php
/**
 * 前台 企业产品管理
 */
class EproductsAction extends Action
{
	/**
	 * 网站统一配置
	 *
	 * @var unknown_type
	 */
	public $Config;
	
	/**
	 * 构造函数
	 *
	 */
	public function _initialize()
	{
		$Config = D('Config');
		$this->Config = $Config->find('config_id=1');
	}
	
	/**
	 * 列表显示所有产品
	 */
	public function Show()
	{
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$Input->get('enter_id')));
		
		//获取企业信息
		$Enterprise = D('Enterprise');
		$Enter = $Enterprise->where($conditions)->find();
		
    	//组成模板 xxx@Index:Show
    	$UserTpl = $Enter['templates'] . '@Eproducts:Show';
		
		$Products = D('Eproducts');
		$count = $Products->count($conditions,'pid');
		$listRows = 15;
		$fields = 'pid,enter_id,name,bimg';
		import('ORG.Util.Page');
		$p = new Page($count,$listRows);
		$list = $Products->findall($conditions,$fields,'pid desc',$p->firstRow .','. $p->listRows);
		$page = $p->show();
		
		//To Tpl
		$this->assign('Enterprise',$Enter);
		$this->assign('enter_id',$Input->get('enter_id'));
		$this->assign('Config',$this->Config);
		$this->assign('Products',$list);
		$this->assign('page',$page);
		$this->display($UserTpl);
	}
	/**
	 * 查看产品详情
	 */
	public function DS()
	{
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		//设置查询条件
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('pid',array('EQ',$Input->get('pid')));
		
		$Eproduct = D('Eproducts');
		$Product = $Eproduct->where($conditions)->find();
		unset($conditions);
		//获取企业信息
		$Enterprise = D('Enterprise');
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$Input->get('enter_id')));
		$Enter = $Enterprise->where($conditions)->find();
		
    	//组成模板 xxx@Index:Show
    	$UserTpl = $Enter['templates'] . '@Eproducts:DS';
    	
		//To Tpl
		$this->assign('Enterprise',$Enter);
		$this->assign('enter_id',$Input->get('enter_id'));
		$this->assign('Config',$this->Config);
		$this->assign('Product',$Product);
		$this->display($UserTpl);
	}
	
	/**
	 * 管理公司所有产品
	 */
	public function UserIndex()
	{
		EasyACL('Eproducts','UserIndex',$_SESSION);
		import('ORG.Util.HashMap');
		import('ORG.Util.Page');
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$_SESSION['enter_id']));
		
		$Product = D('Eproducts');
		//计算所有产品数量
		$count = $Product->count($conditions,'pid');
		$listRows = 15;
		$fields = 'pid,cid,name,bimg,created';
		$p = new Page($count,$listRows);
		$list = $Product->xfindall($conditions,$fields,'pid desc',$p->firstRow . ',' .$p->listRows);
		$page = $p->show();
		//To templates
		$this->assign('Products',$list);
		$this->assign('page',$page);
		$this->assign('Config',$this->Config);
		$this->display();
	}
	
	/**
	 * 创建公司产品
	 */
	public function UserCreate()
	{
		EasyACL('Eproducts','UserCreate',$_SESSION);
		if ($this->isPost()) {
			//上传文件
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			//设置上传文件大小
			$upload->maxSize  = 51200;
			//设置上传文件类型
			$upload->allowExts  = explode(',','jpg,gif,png');
			//设置附件上传目录
			$upload->savePath =  './upload/'; 
			
			//执行上传操作
			if($upload->upload()) {
				$AllFiles = $upload->getSuccessFiles();
				dump($AllFiles);
			}
			
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			
			$Products = D('Eproducts');
			$Products->enter_id = $_SESSION['enter_id'];
			$Products->cid = $Input->post('cid');
			$Products->name = $Input->post('name');
			$Products->bimg = './upload/'.$AllFiles[0]['savename'];
			$Products->videocode = $Input->post('videocode');
			if ($Products->add()) {
				$this->redirect('UserIndex','Eproducts');
			} else {
				return $this->error($Products->getError());
			}

		}
		
		//获取所有的分类
		$Class = D('Ecategory');
		$AllClass = $Class->findall();
		
		//To Templates
		$this->assign('Class',$AllClass);
		$this->assign('Config',$this->Config);
		$this->display();
	}
	/**
	 * 修改产品
	 */
	public function UserUpdate()
	{
		EasyACL('Eproducts','UserUpdate',$_SESSION);
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		
		$Product = D('Eproducts');
		
		import('ORG.Util.HashMap');
		
		//POST
		if ($this->isPost()) {
			
		}
		//获取要修改的产品数据
		$conditions = new HashMap();
		$conditions->put('pid',array('EQ',$Input->get('pid')));
		$Pro = $Product->where($conditions)->find();
		
		$this->assign('Product',$Pro);
		$this->assign('Config',$this->Config);
		$this->display();
	}
	/**
	 * 删除产品
	 */
	public function UserDel($pid)
	{
		EasyACL('Eproducts','UserDel',$_SESSION);
		$Product = D('Eproducts');
		$P = $Product->where('pid='.$pid)->find();
		@unlink($P['bimg']);
		@unlink($P['simg']);
		if ($Product->deleteById($pid)) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	
	public function UserDels()
	{
		EasyACL('Eproducts','UserDels',$_SESSION);
		if ($this->isPost()) {
			$Error = '';
			$Pid = $_POST['pid'];
			foreach ($Pid as $id) {
				if (!$this->UserDel($id)) {
					$Error .= '尝试删除编号为'.$id.'产品失败<br />';
				}
			}
			if (empty($Error)) {
				$this->redirect('UserIndex','Eproducts');
			} else {
				return $this->error($Error);
			}
		}
		
		if ($this->isGet()) {
			$Input = Input::getInstance();
			$Input->filter('addslashes');
			
			if ($this->UserDel($Input->get('pid'))) {
				$this->redirect('UserIndex','Eproducts');
			} else {
				return $this->error('尝试删除产品失败');
			}
		}
	}
}